<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('二次补录页面')" />
    <th:block th:include="include :: jq-viewer-css" />
    <style>
        #forceInput_modal-body{padding-bottom:0px}
        #forceInput_modal-body .box-body{padding:0px}
        #forceInput_modal-body .row>div{padding-right:0px;}
        #forceInput_modal-body .row>div:last-child{padding-right:15px;}
        #imageBox img{width:100%;display:none}
        #sdiImageInput_table tbody td{height:10px;}
        #paeaDsTradeInput_table td{height:10px;}
        .viewer-fullscreen{display: none;}
    </style>
</head>
<body class="white-bg">
<th:block th:include="include :: footer" />
<th:block th:include="include :: jq-viewer-js" />
<th:block th:include="include :: keyMoney-js" />
<div id="forceInput_modal-body" class="modal-body">
    <div class="row">
        <div class="col-md-7" style="padding-right:5px;">
            <div class="box">
                <div class="box-header">
                    <h3 class="box-title">影像信息</h3>
                </div>
                <div class="box-main">
                    <div id="imageBox" style="height:630px;">
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-5">
            <div class="box">
                <div class="box-header">
                    <h3 class="box-title">批次信息</h3><label id="arcCode"></label>
                </div>
                <div class="box-main">
                    <div class="row show-grid">
                        <div class="col-md-6">当日补录量查询:</div><div class="col-md-6"><button class="btn btn-outline btn-info btn-xs" onclick="forceInputWorkload()">查询</button></div>
                        <div class="col-md-6">交易日期：</div><div id="trDate" class="col-md-6">-</div>
                        <div class="col-md-6">交易机构：</div><div id="orgCode" class="col-md-6">-</div>
                        <div class="col-md-6">柜员：</div><div id="tellerCode" class="col-md-6">-</div>
                        <div class="col-md-6">任务总笔数：</div><div id="totalNum" class="col-md-6">-</div>
                        <div class="col-md-6">已补录笔数：</div><div id="inputNum" class="col-md-6">-</div>
                    </div>
                </div>
            </div>
            <div class="box">
                <div class="box-header">
                    <h3 class="box-title">补录信息</h3>
                </div>
                <div class="box-main">
                    <input name="accNo" id="trueAcctNo" class="form-control" type="text" readonly="true">
                    <form class="form-horizontal m" id="form-sdiPaeaArc-add">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">账号：</label>
                            <div class="col-sm-8">
                                <input name="accNo" id="accNo" class="form-control" type="text"
                                       onchange="setAcctNo(this.value);"
                                       onkeyup="setTrueAcctNo()">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">金额：</label>
                            <div class="col-sm-8">
                                <input name="txnAmt" id="txnAmt" class="form-control" type="text" value="0.00"
                                       onfocus="setPos(this,this.value.length);"
                                       onkeydown="javascript:onMoneyKeydown(event, '金额:',this); if(event.keyCode==13&&this.value!=null) submitBatch();"
                                       onkeypress="return (event.keyCode>=48&&event.keyCode<=57)||event.keyCode==13||event.keyCode==9"
                                       onkeyup="onMoneyKeyup(event, '金额:',this)">
                            </div>
                        </div>
                    </form>
                    <!--<button type="button" class="btn btn-outline btn-info">查询账号</button>
                    <button type="button" class="btn btn-outline btn-info">查看全图(F1)</button>-->
                    <button type="button" class="btn btn-outline btn-info" onclick="obtainTask('random')">随机获取任务(F1)</button>
                    <button type="button" class="btn btn-outline btn-info" onclick="obtainTask('paused')">选择列表任务(F2)</button>
                    <button type="button" class="btn btn-outline btn-info" onclick="submitBatch()">提交(F3)</button>
                    <button type="button" class="btn btn-outline btn-info" onclick="closeWindow()">关闭(F4)</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script th:inline="javascript">
    var prefix = '/sdi/input';
    var sdiPaeaArcInput;
    var paeaDsTradeInputList;
    var sdiImageInputList;
    var paeaDsTradeIndex = 0;
    var imgPathPrefix;
    var staticAcctNo;
    var totalNum;
    var inputNum;
    var errorMessage;
    var viewer;

    $(function () {
        //初始化影像显示
        initImageArea();
        //初始化页面数据
        init();
        //初始化影像
        initImage();
        $("#accNo").keydown(function() {
            if(event.keyCode == 13) {
                event.keyCode = 9
            }
        });
    })

    function init() {
        sdiPaeaArcInput = null;
        paeaDsTradeInputList = null;
        sdiImageInputList = null;
        paeaDsTradeIndex = 0;
        imgPathPrefix = null;
        staticAcctNo = null;
        totalNum = null;
        inputNum = null;
        errorMessage = null;
        //初始化补录信息
        initInputArea();
    }

    //初始化影像显示
    function initImageArea() {
        //加载一张默认图片
        $('#imageBox').append('<img name= "img'+'demo'+'" id="imgArea" src="" alt="示例图片">');
        $('#imageBox').viewer({inline:true}).viewer('update');
    }

    //初始化影像
    function initImage() {
        var imgUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1584958911886&di=47b6a313b01424ad2593609115dbd145&imgtype=0&src=http%3A%2F%2Fbbsfiles.vivo.com.cn%2Fvivobbs%2Fattachment%2Fforum%2F201507%2F31%2F161843qkvv4oq21nrvtjui.jpg";
        $('#imgArea').attr("src", imgUrl);
        $('#imageBox').viewer({inline:true}).viewer('update');
    }

    //初始化补录信息
    function initInputArea(){
        //账号输入框
        $('#accNo').val();
        //金额输入框
        $('#txnAmt').val();
    }

    //打开选择任务页面，获取任务
    function obtainTask(type) {
        if(sdiPaeaArcInput!=null) {
            if (!confirm("当前批次未完成补录，确定要选择新批次吗?")) {
                return;
            }
        }
        if(type=='random'){
            setTimeout('obtainTaskByid()',100);
        } else if(type=='paused'){
            g_arc_id = window.showModalDialog(prefix+'/pausedTask/forceInput',window,'dialogWidth=900px; dialogHeight=600px; help=no; status=no; scroll=yes; resizable=no;');
            if(g_arc_id != null)
            {
                setTimeout('obtainTaskByid("'+g_arc_id+'")',100);
            }
        }
    }

    var g_isRealSubmit = false;
    function obtainTaskByid(arcId) {
        if ( g_isRealSubmit ==true) return false;
        g_isRealSubmit = true;
        var url = prefix+"/forceInputBatchInfo";
        if(arcId){
            url= url+"/"+arcId;
        } else {
            url= url+"/random";
        }
        $.operate.get(url,function(result){
            var forceInputBatchInfo = result.data;
            if(!forceInputBatchInfo || !forceInputBatchInfo.sdiPaeaArcInput ){
                $.modal.msgWarning('当前没有任务！');
                g_isRealSubmit = false;
                return;
            }
            //init();
            //赋值批次信息
            sdiPaeaArcInput = forceInputBatchInfo.sdiPaeaArcInput;
            imgPathPrefix = sdiPaeaArcInput.imageUrlPrefix;
            $('#arcCode').text(sdiPaeaArcInput.code);
            //赋值流水信息
            paeaDsTradeInputList = forceInputBatchInfo.paeaDsTradeInputList;
            fillSdiPaeaArcDetail(sdiPaeaArcInput,paeaDsTradeInputList);
            loadNext(paeaDsTradeInputList[0]);
            paeaDsTradeIndex = 0;
            g_isRealSubmit = false;
        })
    }

    //展示批次信息
    function fillSdiPaeaArcDetail(sdiPaeaArcInput,paeaDsTradeInputList) {
        $("#trDate").text(sdiPaeaArcInput.trDate);
        $("#orgCode").text(sdiPaeaArcInput.orgCode);
        $("#tellerCode").text(sdiPaeaArcInput.tellerCode);
        $("#totalNum").text(paeaDsTradeInputList.length);
        inputNum = 0;
        $("#inputNum").text(inputNum);
        totalNum= paeaDsTradeInputList.length;
    }
    //更新影像显示
    function loadNext(paeaDsTradeInput){
        $("#inputNum").text(inputNum);
        $("#accNo").val('');
        $("#txnAmt").val('0.00');
        $("#trueAcctNo").val('');
        errorMessage='';
        sdiImageInputList = paeaDsTradeInput.sdiImageInputList;
        $('#imageBox').empty();
        $.each(sdiImageInputList, function(index, sdiImageInput) {
            var imageUrl = 'http://'+imgPathPrefix +sdiImageInput.sdifilepath;
            $('#imageBox').append('<img name= "img'+index+'" src="'+imageUrl+'" alt="影像">');
        });
        $('#imageBox').viewer({inline:true}).viewer('update');
        staticAcctNo = paeaDsTradeInput.accNo.substring(0,paeaDsTradeInput.accNo.length-4)
    }

    function setTrueAcctNo() {
        var tempAcctNo = $("#accNo");
        if(tempAcctNo==""||tempAcctNo==null){
            return;
        }
        var temp=tempAcctNo.val();
        if (temp =="") {
            $("#trueAcctNo").val(staticAcctNo);
        } else {
            $("#trueAcctNo").val(setAcctNo(temp));
        }
    }

    function setAcctNo(accNo){
        var tempItemAcctNo=accNo;
        if(tempItemAcctNo==null||tempItemAcctNo==""){
            return;
        }
        if(tempItemAcctNo.length<=4){
            var prefixAcctNo=staticAcctNo;
            tempItemAcctNo=prefixAcctNo+tempItemAcctNo;
        }else{
            tempItemAcctNo=tempItemAcctNo;
        }
        return tempItemAcctNo;
    }

    /**
     *单笔提交
     **/
    var g_isSumbitting = false;
    function submitBatch() {
        if(sdiPaeaArcInput==null) {
            return false;
        }
        if (g_isSumbitting){
            alert("不能重复提交！");
            return false;
        }
        g_isSumbitting = true;
        var accNo = $("#trueAcctNo").val();
        var amt = $("#txnAmt").val();

        if (paeaDsTradeIndex>=totalNum) {
            paeaDsTradeIndex=totalNum-1;
        }
        if(checkInput(accNo,amt) == false) {
            g_isSumbitting = false;
            return false;
        }
        paeaDsTradeInputList[paeaDsTradeIndex].inputed=true;
        if (paeaDsTradeIndex>=totalNum-1) {
            submit();
            g_isSumbitting = false;
            return;
        } else {
            submit1();
        }
        g_isSumbitting = false;
    }

    /**
     *检查输入金额的正确性
     **/
    function checkInput(accNo,amt) {
        var trade =paeaDsTradeInputList[paeaDsTradeIndex];
        if(trade==null){
            alert("流水为空！");
            return false;
        }
        if(accNo==null){
            alert('请输入账号!');
            $("#accNo").focus();
            return false;
        }

        if(amt==null){
            alert('请输入金额!');
            $("#txnAmt").focus();
            return false;
        }
        amt = amt.replace(',','');
        if (accNo != trade.accNo && amt != trade.txnAmt) {
            if(confirm("输入的账号、金额与流水账号和金额都不相符，确认提交将登记一条风险，确认提交？\n"+ trade.accNo + "," + trade.txnAmt+","+ trade.vchNo)){
                errorMessage = "输入的账号、金额与流水账号和金额都不相符，输入的账号为" + accNo + ", 输入的金额为"+amt+",流水金额为"+trade.txnAmt;
                return true;
            } else {
                $("#accNo").focus();
                return false;
            }
        } else if (accNo != trade.accNo) {
            if(confirm("输入的账号与流水账号不相符，确认提交将登记一条风险，确认提交？")){
                errorMessage = "输入的账号与流水账号不相符，输入的账号为" + accNo+"，流水账号="+trade.accNo;
                return true;
            } else {
                $("#accNo").focus();
                return false;
            }
        } else if ( amt != trade.txnAmt) {
            if(confirm("输入的金额与流水金额不相符，确认提交将登记一条风险？")) {
                errorMessage = "输入的金额与流水金额不相符，输入的金额为" + amt;
                return true;
            } else {
                $("#txnAmt").focus();
                return false;
            }
        }
        return true;
    }

    //提交单笔
    function submit1() {
        $.ajax({
            type : "post",
            url : prefix+"/submitForceInputBatch",
            data : JSON.stringify({
                isCommit: false,
                errorMessage: errorMessage,
                paeaDsTradeInput: paeaDsTradeInputList[paeaDsTradeIndex]}),
            contentType:"application/json",
            dataType : "json",
            success : function(result) {
                if (result.code == web_status.SUCCESS) {
                    paeaDsTradeIndex++;
                    inputNum++;
                    if(inputNum+1 <= totalNum){
                        loadNext(paeaDsTradeInputList[paeaDsTradeIndex]);
                    }
                } else if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                } else {
                    $.modal.alertError(result.msg);
                }
            }
        });
    }

    /**
     *任务提交
     **/
    function submit() {
        $.ajax({
            type : "post",
            url : prefix+"/submitForceInputBatch",
            data : JSON.stringify({
                isCommit: true,
                sdiPaeaArcInput: sdiPaeaArcInput,
                errorMessage: errorMessage,
                paeaDsTradeInput: paeaDsTradeInputList[paeaDsTradeIndex]}),
            contentType:"application/json",
            dataType : "json",
            success : function(result) {
                if (result.code == web_status.SUCCESS) {
                    clearAllTaskInfo();
                    if(confirm("提交成功，继续取下一批任务？")) {
                        obtainPausedTask()
                    }
                } else if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                } else {
                    $.modal.alertError(result.msg);
                }
            }
        });
    }

    /**
     *页面初始化
     **/
    function clearAllTaskInfo() {
        sdiPaeaArcInput = null;
        paeaDsTradeInputList = null;
        sdiImageInputList = null;
        paeaDsTradeIndex = 0;
        imgPathPrefix = null;
        staticAcctNo = null;
        errorMessage = '';
        clearSdiPaeaArcDetail();
    }

    //清除批次信息
    function clearSdiPaeaArcDetail() {
        $("#trDate").text('');
        $("#orgCode").text('');
        $("#tellerCode").text('');
        $("#totalNum").text('');
        $("#inputNum").text('');
        inputNum = 0;
        totalNum = 0;
    }

    function closeWindow(){
        if(sdiPaeaArcInput!=null){
            if (confirm("当前有批次正常处理，确定关闭？")) {
                window.close();
            } else {
                return;
            }
        }
        window.close();
    }

    function forceInputWorkload(){
        $.operate.get(prefix+'/forceInputWorkload');
    }
</script>
</body>
</html>